{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "企业上链"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-09-05 16:32:48,080 - modelscope - INFO - PyTorch version 2.3.0 Found.\n",
      "2024-09-05 16:32:48,081 - modelscope - INFO - Loading ast index from /home/jie/.cache/modelscope/ast_indexer\n",
      "2024-09-05 16:32:48,095 - modelscope - INFO - Loading done! Current index file version is 1.14.0, with md5 9f53011443b7fabdfb807148bea6e1f2 and a total number of 976 components indexed\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from datasets import load_dataset\n",
    "\n",
    "from prompt import get_industry_trans_func\n",
    "from utils import glm4_vllm, load_obj\n",
    "from setting import StaticValues\n",
    "from parse import reason_label_parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sv = StaticValues(\"hydrogen\")\n",
    "logger = sv.logger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人', '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号', '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接', '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度', 'reason', 'label', 'industry_vllm_prompt'],\n",
       "    num_rows: 2\n",
       "})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "attr_name = \"industry_vllm_prompt\"\n",
    "# 导入关键词分类的表\n",
    "dataset = load_dataset(\"csv\", data_files=sv.BINARY_CSV, split=\"train\")\n",
    "new_dataset = dataset.map(\n",
    "    get_industry_trans_func(attr_name, sv.industry_cls_prompt),\n",
    "    )\n",
    "new_dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompts = []\n",
    "for item in new_dataset:\n",
    "    prompts.append([{\"role\": \"user\", \"content\": item[attr_name]}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装一下，如果已经数据，自动导入\n",
    "glm4_vllm(prompts, output_dir=sv.INDUSTRY_VLLM_OBJ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "vllm_output = load_obj(sv.INDUSTRY_VLLM_OBJ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024/09/05 16:36:03 - hydrogen - INFO: \n",
      "分类结果统计：{('应用领域',): 2}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "reasons = []\n",
    "labels = []\n",
    "\n",
    "d = {}\n",
    "\n",
    "for _, pred in enumerate(vllm_output):\n",
    "    pred_text = pred.outputs[0].text\n",
    "    reason, label = reason_label_parse(pred_text)\n",
    "    label_tmp = []\n",
    "\n",
    "    for item in sv.LABEL_NAME:\n",
    "        if item in label:\n",
    "            label_tmp.append(item)\n",
    "\n",
    "    label_tmp.sort()\n",
    "\n",
    "    k = tuple(label_tmp)\n",
    "    if k in d:\n",
    "        d[k] += 1\n",
    "    else:\n",
    "        d[k] = 1\n",
    "    reasons.append(reason)\n",
    "    labels.append(label_tmp)\n",
    "\n",
    "logger.info(f\"分类结果统计：{d}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024/09/05 16:36:05 - hydrogen - INFO: \n",
      "分类结果统计：{('应用领域',): 2}\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "logger.info(f\"分类结果统计：{d}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人', '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号', '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接', '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度'],\n",
       "    num_rows: 2\n",
       "})"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2931c184dc4a4e63acabd86061e9a57e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating CSV from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "4020"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导出excel\n",
    "# dataset =dataset.remove_columns([\"reason\", \"label\"])\n",
    "tmp_dataset = dataset.add_column(\"reason\", reasons)\n",
    "tmp_dataset = tmp_dataset.add_column(\"label\", labels)\n",
    "# 下标筛选\n",
    "tmp_dataset.to_csv(sv.INDUSTRY_CSV, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人', '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号', '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接', '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度'],\n",
       "    num_rows: 2\n",
       "})"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
